using Microsoft.AspNetCore.Mvc;
using ShopForWeb.Dto.AjaxModels;
using ShopForWeb.Dto.ViewModels;
using ShopForWeb.Service;
using ShopForWeb.UI.Models;
using ShopForWeb.UI.Extensions;
using ShopForWeb.UI.Filters;

namespace ShopForWeb.UI.Controllers;

[Route("commoditys")]
public class CommodityController : Controller
{
  private CommodityService _service;
  public CommodityController(CommodityService service)
  {
    _service = service;
  }

  [UserInfoFiler]
  [HttpGet("{page?}/{pageSize?}/{key?}")]
  public async Task<IActionResult> GetPageAsync([ModelBinder(Name = "page")] int page = 1, [ModelBinder(Name = "pageSize")] int pageSize = 12, [ModelBinder(Name = "key")] string key = "")
  {
    var commoditys = await _service.GetByPageAsync((page - 1) * pageSize, pageSize, key);
    var total = await _service.GetCountAsync(key);
    PageBarViewModel pagebar = new PageBarViewModel(page, pageSize, total);
    ViewBag.Data = new
    {
      Commoditys = commoditys,
      PageBar = pagebar
    };
    return View();
  }

  [UserInfoFiler]
  [HttpGet("info/{commodityID}")]
  public async Task<IActionResult> GetByIDAsync(int commodityID)
  {
    var p = await _service.GetByIDAsync(commodityID);
    ViewBag.Data = p;
    return View();
  }
}
